#!/usr/bin/python
# -*- coding: UTF-8 -*-
from git import Repo
import os
import shutil
import time
import threading

def progress(op_code, cur_count, max_count=None, message=''):
    print (str(cur_count) + ":" + str(max_count))
    print (message)


# repo = Repo("/media/workspace/liuwei")
# repo = Repo.init("/media/workspace/liuwei",True,bare=False);
# repo = Repo.clone_from("https://github.com/gitpython-developers/GitPython.git","/media/workspace/liuwei",progress)
def makeApk():
    outPath = "/home/ubuntu/liuwei/Novel2"
    localFile = "local.properties"
    srcproperty = "/home/ubuntu/" + localFile
    desproperty = outPath + "/" + localFile

    if os.path.exists(outPath):
        shutil.rmtree(outPath)
    repo = Repo.clone_from("https://hnyjliuwei:coco5200@gitee.com/hnyjliuwei/Novel2.git",outPath,progress)
    print ("git clone over")

    shutil.copy(srcproperty,desproperty)

    os.chdir(outPath)
    print (os.getcwd())

    # os.system("chmod +x gradlew")
    os.system("./gradlew assembleRelease")

    timeStr = time.strftime("%Y%m%d%H%M%S",time.localtime())
    print (timeStr)

    srcapk = outPath + "/app/build/outputs/apk/app-release.apk"
    desapkDir = "/usr/tomcat/webapps/apks"
    desapkName="novel-" + timeStr + ".apk"

    if not os.path.exists(desapkDir):
        os.makedirs(desapkDir)

    shutil.copy(srcapk,desapkDir + "/" + desapkName)

t = threading.Thread(target=makeApk)
t.setDaemon(False)
t.start()

